package com.cqan.common;

import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.format.ScriptStyle;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import com.cqan.entity.Atom;
import com.cqan.entity.AtomField;
import com.cqan.entity.AtomValue;
import com.cqan.entity.FieldType;
import com.cqan.entity.Report;

public class ExportUtils {

	public static void export(Report report, List<AtomField> fields,
			List<Atom> atoms, OutputStream out) throws Exception {
		WritableWorkbook book = Workbook.createWorkbook(out);
		// 生成名为“报表名称”的工作表，参数
		WritableSheet sheet = book.createSheet(report.getName(), 0);
		// 设置表头表格
		sheet.mergeCells(0, 0, fields.size()-1, 0);
		Label label = new Label(0, 0, report.getName(), setTitleStyle());
		sheet.addCell(label);
		//添加表头
		for (int i = 0; i < fields.size(); i++) {
			AtomField field = fields.get(i);
			Label header = new Label(i, 1, field.getName(),setStyle(13,true));
			sheet.addCell(header);
		}
		//添加数据
		for (int i = 0; i < atoms.size(); i++) {
			for (int j = 0; j < fields.size(); j++) {
				Atom atom = atoms.get(i);
				Map<AtomField, AtomValue> rows = atom.getRows();
				AtomField field = fields.get(j);
				if (rows.containsKey(field)) {
					Object value = rows.get(field).getFieldValue();
					Label content = null;
					if (value!=null) {
						if (field.getFieldType().equals(FieldType.D)) {
						SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
							String date = sdf.format((Date)rows.get(field).getFieldValue());
							content = new Label(j,i+2,date,setStyle(12,false));
							sheet.addCell(content);
						}else{
							content = new Label(j,i+2,value.toString(),setStyle(12,false));
							sheet.addCell(content);
						}
					}else{
						content = new Label(j,i+2,"无",setStyle(12,false));
						sheet.addCell(content);
					}
				}
			}
		}
		book.write();  
		book.close();  
	}
	
	private static WritableCellFormat setStyle(int fontSize,boolean bold) throws Exception {
		WritableFont he = new WritableFont(WritableFont.createFont("仿宋_GB2312"),// 字体
				fontSize,// WritableFont.DEFAULT_POINT_SIZE, // 字号
				WritableFont.NO_BOLD, // 粗体
				false, // 斜体
				UnderlineStyle.NO_UNDERLINE, // 下划线
				Colour.BLACK, // 字体颜色
				ScriptStyle.NORMAL_SCRIPT);
		if (bold) {
			he.setBoldStyle(WritableFont.BOLD);
		}
		WritableCellFormat wcf = new WritableCellFormat(he);
		wcf.setAlignment(Alignment.CENTRE); // 设置对齐方式
		return wcf;
	}

	private static WritableCellFormat setTitleStyle() throws Exception {
		WritableFont he = new WritableFont(WritableFont.createFont("仿宋_GB2312"),// 字体
				20,// WritableFont.DEFAULT_POINT_SIZE, // 字号
				WritableFont.BOLD, // 粗体
				false, // 斜体
				UnderlineStyle.NO_UNDERLINE, // 下划线
				Colour.BLACK, // 字体颜色
				ScriptStyle.NORMAL_SCRIPT);
		WritableCellFormat wcf = new WritableCellFormat(he);
		wcf.setAlignment(Alignment.CENTRE); // 设置对齐方式
		return wcf;
	}
}
